﻿Imports System
Imports System.ComponentModel
Imports System.Globalization
Imports System.Workflow.ComponentModel

Namespace System.Workflow.Activities.Rules
    Public NotInheritable Class UpdatedConditionAction
        Inherits RuleConditionChangeAction
        ' Methods
        Public Sub New()
        End Sub

        Public Sub New(ByVal conditionDefinition As RuleCondition, ByVal newConditionDefinition As RuleCondition)
            If (conditionDefinition Is Nothing) Then
                Throw New ArgumentNullException("conditionDefinition")
            End If
            If (newConditionDefinition Is Nothing) Then
                Throw New ArgumentNullException("newConditionDefinition")
            End If
            If (newConditionDefinition.Name <> conditionDefinition.Name) Then
                Throw New ArgumentException(String.Format(CultureInfo.CurrentCulture, Messages.ConditionNameNotIdentical, New Object() { newConditionDefinition.Name, conditionDefinition.Name }))
            End If
            Me._conditionDefinition = conditionDefinition
            Me._newConditionDefinition = newConditionDefinition
        End Sub

        Protected Overrides Function ApplyTo(ByVal rootActivity As Activity) As Boolean
            If (rootActivity Is Nothing) Then
                Return False
            End If
            Dim definitions As RuleDefinitions = [TryCast](rootActivity.GetValue(RuleDefinitions.RuleDefinitionsProperty),RuleDefinitions)
            If ((definitions Is Nothing) OrElse (definitions.Conditions Is Nothing)) Then
                Return False
            End If
            If (definitions.Conditions.Item(Me.ConditionDefinition.Name) Is Nothing) Then
                Return False
            End If
            Dim flag As Boolean = False
            If definitions.Conditions.RuntimeMode Then
                definitions.Conditions.RuntimeMode = False
                flag = True
            End If
            Try 
                definitions.Conditions.Remove(Me.ConditionDefinition.Name)
                definitions.Conditions.Add(Me.NewConditionDefinition)
            Finally
                If flag Then
                    definitions.Conditions.RuntimeMode = True
                End If
            End Try
            Return True
        End Function


        ' Properties
        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        Public Property ConditionDefinition As RuleCondition
            Get
                Return Me._conditionDefinition
            End Get
            Set(ByVal value As RuleCondition)
                If (value Is Nothing) Then
                    Throw New ArgumentNullException("value")
                End If
                Me._conditionDefinition = value
            End Set
        End Property

        Public Overrides ReadOnly Property ConditionName As String
            Get
                Return Me._conditionDefinition.Name
            End Get
        End Property

        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        Public Property NewConditionDefinition As RuleCondition
            Get
                Return Me._newConditionDefinition
            End Get
            Set(ByVal value As RuleCondition)
                If (value Is Nothing) Then
                    Throw New ArgumentNullException("value")
                End If
                Me._newConditionDefinition = value
            End Set
        End Property


        ' Fields
        Private _conditionDefinition As RuleCondition
        Private _newConditionDefinition As RuleCondition
    End Class
End Namespace

